package com.bagus.mediapipefacedetection.manager;

public class FpsManager {
    private static final String TAG = "FpsManager";
    private static final float  DEFAULT_DINOISE = 0.2f;
    private static final int LIMITE_COUNT_FLAG = 10;  //计数计算节点
    private static final int LIMITE_FPS = 15;  //降级的帧率

    private int curFps = 60;
    private long startTime = 0;
    private long endTime = 0;
    private int count = 0;
    private volatile int fpsThreshold = 0;

    private static boolean isFinishChecking = false;

    private static volatile FpsManager instance = null;

    private FpsManager() {
    }

    public static FpsManager getInstance() {
        if (instance == null) {
            synchronized (FpsManager.class) {
                if (instance == null) {
                    instance = new FpsManager();
                }
            }
        }
        return instance;
    }

    public void init(){
        count = 0;
        startTime = 0;
        endTime = 0;
        curFps = 60;
        fpsThreshold = LIMITE_FPS;
    }

    public int getCurFps() {
        return curFps;
    }

    public void checkFps() {

        if (startTime == 0) {
            startTime = System.currentTimeMillis();
        }

        if (count == LIMITE_COUNT_FLAG) {
            curFps = (int) (1000 * count / (endTime - startTime));

            count = 0;
            startTime = System.currentTimeMillis();
        } else {
            endTime = System.currentTimeMillis();
            count++;
        }
    }
}
